7224
10108
Elenco dei contatti dal servizio Web e lo visualizzo nella tabella "sezionata" dei contatti come si vede nello screenshot.
Il problema è che ottengo gli stessi valori di tag per le caselle di controllo della prima riga per la sezione A e per la sezione S. Ho ordinato un array e visualizzato nella vista tabella indicizzata. Come ottenere diversi valori di tag a seconda di indexPath.row indipendentemente dal numero di sezioni visualizzate. Ecco cosa ho provato
In cellForRowAtIndexPath:
UIButton * checkBox;
if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton alloc] initWithFrame: CGRectMake (7, 8, 30, 30)];
}
altro
{
checkBox = [[UIButton alloc] initWithFrame: CGRectMake (15, 13, 30, 30)];
}
// int cnt = 0;
// for (int i = indexPath.section - 1; i> 0; i--)
// {
// cnt + = [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: i]] count]; // arrayOfCharachters contiene i caratteri dalla "A" alla "Z"
//}
//checkBox.tag = cnt + indexPath.row;
[checkBox setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal];
[checkBox addTarget: self action: @selector (checkBoxClicked :) forControlEvents: UIControlEventTouchUpInside];
[checkBox setTag: indexPath.row];
[cell.contentView addSubview: checkBox];
cella di ritorno;
}
- (void) checkBoxClicked: (id) mittente
{
CGPoint buttonPosition = [mittente convertPoint: CGPointZero toView: self.tableViewContact];
NSIndexPath * indexPath = [self.tableViewContact indexPathForRowAtPoint: buttonPosition];
UIButton * tappedButton = (UIButton *) mittente;
NSLog (@ "Numero tag =% d", [tag mittente]);
if ([tappedButton.currentImage isEqual: [UIImage imageNamed: @ "checkBox.png"]])
{
[sender setImage: [UIImage imageNamed: @ "checkBoxMarked.png"] forState: UIControlStateNormal];
if (indexPath! = Nil)
{
NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row]; // memorizza gli ID delle caselle di controllo in mutableArrayOfIds
NSLog (@ "Pulsante contrassegnato con tag id =% @", finalIntId);
[arrayOfIds addObject: finalIntId];
}
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex: tappedButton.tag];
// NSString * finalIntId = [mutableArrayOfIds objectAtIndex: indexPath.row];
}
altro
{
[sender setImage: [UIImage imageNamed: @ "checkBox.png"] forState: UIControlStateNormal];
NSLog (@ "UnChecked");
// [arrayOfIds removeObjectAtIndex: tappedButton.tag];
}
}
- (NSString *) tableView: (UITableView *) tableView titleForHeaderInSection: (NSInteger) sezione {
if ([arrayOfCharacters count] == ​​0)
{
ritorno @"";
}
return [NSString stringWithFormat: @ "% @", [arrayOfCharacters objectAtIndex: section]];
}
- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
NSArray * toBeReturned = [NSArray arrayWithArray:
[@ "A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | # "
componentsSeparatedByString: @ "|"]];
return toBeReturned;
}
- (NSInteger) tableView: (UITableView *) tableView sectionForSectionIndexTitle: (NSString *) title atIndex: (NSInteger) index
{
Conteggio NSInteger = 0;
for (NSString * character in arrayOfCharacters) {
if ([character isEqualToString: title]) {
conteggio dei resi;
}
count ++;
}
return 0;
}
- (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
{
return [arrayOfCharacters count];
// return 1;
}
- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) sezione
{
// return [mutableArray count];
return [[objectsForCharacters objectForKey: [arrayOfCharacters objectAtIndex: sezione]] count];
} 
stai impostando lo stesso valore di tag per tutte le sezioni che hanno la stessa riga.
IndexPath ha due proprietà, {section, row}.
Diciamo che la sezione A ha due righe,
per riga1 -> indexPath.section = 0, indexPath.row = 0;
per row2-> indexPath.section = 0, indexPath.row = 1;
Diciamo che la sezione S ha due righe,
per riga1 -> indexPath.section = 1, indexPath.row = 0;
per row2-> indexPath.section = 1, indexPath.row = 1;
Quindi, per la riga1 della sezione A e la riga1 della sezione S, stai impostando lo stesso valore di tag che è 0. C'è il tuo problema.
Prova a impostare il valore del tag come di seguito.
button.tag = indexPath.section * 1000 + indexPath.row;
durante il recupero della sezione e della riga,
Sezione NSInteger = (button.tag) / 1000;
NSInteger row = (button.tag)% 1000;
|
Prova questo...
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
static NSString * CellIdentifier = @ "Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
UIButton * checkBox = [[UIButton alloc] initWithFrame: CGRectMake (280, 10, 50, 44)];
checkBox.backgroundColor = [UIColor orangeColor];
[checkBox addTarget: self action: @selector (checkBoxClicked: event:) forControlEvents: UIControlEventTouchUpInside];
[checkBox setTag: indexPath.row];
[cell.contentView addSubview: checkBox];
cella di ritorno;
}
- (void) checkBoxClicked: (id) sender event: (id) event
{
NSSet * touches = [event allTouches];
UITouch * touch = [tocca un oggetto];
CGPoint currentTouchPosition = [touch locationInView: self.tv]; // qui tv è TableView Object
NSIndexPath * indexPath = [self.tv indexPathForRowAtPoint: currentTouchPosition];
NSLog (@ "valore di indePath.section% d, indexPath.row% d", indexPath.section, indexPath.row);
}
|
Ciò sta accadendo perché stai assegnando tag a pulsanti INDIPENDENTI dalle sezioni.
Entrambe le prime righe delle sezioni A e S hanno riga = 0. quindi il tag assegnato al rispettivo pulsante è 0. È necessario assegnarli mantenendo il riferimento alle sezioni.
Suggerirei di assegnare un suggerimento di accessibilità con un modulo separato da virgole contenente Sezione, Riga.
E nel tuo metodo
- (void) checkBoxClicked: (id) mittente
{
// seleziona una stringa dal formato separato da virgole. Il primo è la tua sezione, il secondo è la riga.
}
la seconda opzione è Fai quello che fai e implementa il tuo metodo Button in questo modo.
CGPoint buttonPosition = [mittente convertPoint: CGPointZero toView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: buttonPosition];
if (indexPath! = nil)
{
... indexpath.section è la tua sezione, index path.row è la tua riga.
}
C'è anche la terza opzione.
in cellforRowAtIndexpath assegna un titolo al tuo Button
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
[btn setTitle: <# Your Row #> forState: UIControlStateDisabled];
[btn setTag <#Your Section #>];
quindi dopo aver ricevuto nel tuo metodo Button puoi avere sia Sezione (Tag) che Riga (Titolo per stato disabilitato).
- (void) checkBoxClicked: (id) sender {[button titleForState: UIControlStateDisabled]; // la tua riga
button.tag // la tua sezione}
|
Prova questo!
Ogni sezione potresti conoscere il conteggio della sezione a destra, quindi aggiungere il conteggio della singola riga.
[[fieldTitlesList objectAtIndex: indexPath.section - 1] count] + indexPath.row
Dove fieldTitlesList è l'array delle tue sezioni.
|
Ho aggiunto il seguente codice che ha risolto il mio problema
NSInteger rowNumber = 0;
for (NSInteger i = 0; i